package com.framework.foundation.common.run;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.framework.foundation.handler.GlobalHandler;
import com.framework.foundation.interceptor.AdminInterceptor;
import com.framework.foundation.system.dictionary.Dictionary;
import com.framework.foundation.system.dictionary.DictionaryDetail;
import com.framework.foundation.system.user.User;
import com.framework.foundation.util.sqlxml.SqlInXmlPlugin;
import com.framework.foundation.weixin.wxdevice.Wxdevice;
import com.framework.foundation.weixin.wxmac.Wxmac;
import com.framework.foundation.weixin.wxstore.Wxstore;
import com.framework.foundation.weixin.wxuser.Wxuser;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.weixin.sdk.api.ApiConfigKit;

public class GlobalConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants constants) {
		PropKit.use("init.properties");
		constants.setDevMode(PropKit.getBoolean("devMode", false));

		constants.setEncoding("UTF-8");

		// 设置全局视图的基础路径
		// 注意：跳转视图时已"/"开头的是从跟路径下找页面,""开头的是从baseViewPath + viewPath + renderPath
		// 其中viewPath默认为controlerKey
		// constants.setBaseViewPath("/view");

		constants.setError404View("/common/404.html");
		constants.setError500View("/common/500.html");

		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(constants.getDevMode());
	}

	@Override
	public void configRoute(Routes routes) {
		// 配置后台请求规则
		routes.add(new AdminRoutes());
		routes.add(new WxRoutes());
	}

	@Override
	public void configPlugin(Plugins plugins) {
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("db.jdbcUrl"), PropKit.get("db.username"),
				PropKit.get("db.password"), PropKit.get("db.driverClass"));
		// 配置Druid数据库连接池大小
		druidPlugin.set(PropKit.getInt("db.initialSize"), PropKit.getInt("db.minIdle"), PropKit.getInt("db.maxActive"));
		// 配置Druid数据库连接池过滤器配制
		druidPlugin.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType(JdbcConstants.MYSQL);
		WallConfig config = new WallConfig();
		config.setFunctionCheck(false); // 支持数据库函数
		wall.setConfig(config);
		druidPlugin.addFilter(wall);
		plugins.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setDevMode(PropKit.getBoolean("devMode", false)); // 设置开发模式
		arp.setShowSql(PropKit.getBoolean("devMode", false)); // 是否显示SQL
		// arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));// 大小写敏感
		plugins.add(arp);

		// sql语句plugin, 默认从classpath下面开始查找以sql.xml结尾的配置文件
		plugins.add(new SqlInXmlPlugin());

		// 表和模型的映射关系
		arp.addMapping("ww_user", User.class);
		arp.addMapping("ww_dictionary", Dictionary.class);
		arp.addMapping("ww_dictionary_detail", DictionaryDetail.class);

		// 微信相关映射关系
		arp.addMapping("ww_wxuser", Wxuser.class);
		arp.addMapping("ww_store", Wxstore.class);
		arp.addMapping("ww_device", Wxdevice.class);
		arp.addMapping("ww_mac", Wxmac.class);
	}

	@Override
	public void configInterceptor(Interceptors interceptors) {
		// 添加seesion拦截器
		interceptors.add(new SessionInViewInterceptor());

		// 添加后台拦截器
		interceptors.add(new AdminInterceptor("/", new String[] { "/login", "/weixin" }));

	}

	@Override
	public void configHandler(Handlers handlers) {
		handlers.add(new GlobalHandler());
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 9090, "/", 5);
	}
}
